<?php
class MPayHistory extends MY_Model {

    protected $table = 'pay_history';

    function __construct() {
        parent::__construct();
    }

    public function getAllShops(){
        $this->db->select( $this->table.'.*');
        $this->db->distinct();
        $this->db->from($this->table);
        if($this->_user->role_id == 2 || $this->_user->role_id == 4){
            $this->db->where('group_id',$this->_user->group_id);
        }

        $query = $this->db->get();
        $queryResult =  $query->result();
        $kvArr = array();
        foreach ($queryResult as $k=>$v){
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    public static $requestStatus = Array(
        0 => '<span class="label label-warning">财务待处理</span>',
        1 => '<span class="label label-success">处理全部完成</span>',
        2 => '<span class="label label-warning">部分未处理完成</span>',
        3 => '<span class="label label-danger">申请记录信息有误</span>',
        4 => '<span class="label label-info">测评修改后重新提交</span>',
    );

    public static function getRStatus($method=''){
        if($method === ''){
            return self::$requestStatus;
        }
        return element($method, self::$requestStatus, '');
    }

    public function getList($limit, $sort=false){
        $this->db->select( '*');
        $this->db->distinct();
        $this->db->from($this->table);
        $this->db->limit($limit['limit'],$limit['offset']);
        $this->formfilter->doFilter();
        if($sort){
            $this->db->order_by('sort', 'asc');
        }else{
            $this->db->order_by('id', 'desc');
        }
        $query = $this->db->get();
        return $query->result();
    }
    public function getCount(){
        $this->db->select('count(distinct id) as num');
        $this->db->from($this->table);
        $this->formfilter->doFilter();
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getById($id){
        $query = $this->db->where('id',$id)->get($this->table);
        return $query->row();
    }

    /**
     * @param array
     * @return object
     * excel 表格导出查询数据
     */
    public function exportList($where)
    {
        $this->db->select("h.*,a.name as f_name,p.name as p_name,p.account as p_account,(h.pay_amount/h.rate) as pay_coin_type_amount");
        $this->db->from($this->table.' h');
        $this->db->join('admins a', 'h.finance_id = a.id', 'LEFT');
        $this->db->join('pay_account p', 'h.pay_account = p.id', 'LEFT');

        if ($begintime = $where['finance_his_create_time_begin']) {
            $this->db->where(array('create_time >= ' => strtotime($begintime . ' 00:00:00')));
        }

        if ($endtime = $where['finance_his_create_time_end']) {
            $this->db->where(array('create_time <= ' => strtotime($endtime . ' 23:59:59')));
        }

        if ($where['finance_id']) {
            $this->db->where('finance_id', $where['finance_id']);
        }
        if ($where['pay_account']) {
            $this->db->where('pay_account', $where['pay_account']);
        }
        if ($where['receive_account']) {
            $this->db->where('receive_account', $where['receive_account']);
        }

        $result = $this->db->get()->result();

        foreach ($result as $k => $v) {
            $v->pay_coin_type_amount = number_format($v->pay_coin_type_amount,3,'.','').'('.IOSS_StaticConfig::getcoinTypeInfo($v->coin_type).')';
            $v->pay_account = $v->p_name.'-'.$v->p_account;
            $v->finance_id = $v->f_name;
            $v->pay_amount = $v->pay_amount.'(RMB)';
            $v->coin_type = IOSS_StaticConfig::getcoinTypeInfo($v->coin_type);
            if ($v->pay_request_id){
                $v->pay_request_id = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/finance/apply/detail/'.$v->pay_request_id;
            }else{
                $v->pay_request_id = '';
            }
            if ($v->task_operate_id){
                $v->task_operate_id = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/tasks/taskoperate/ajaxViewDetail/'.$v->task_operate_id;
            }else{
                $v->task_operate_id = '';
            }
            $v->create_time = date('Y-m-d H:i:s', $v->create_time);
            $v->update_time = date('Y-m-d H:i:s', $v->update_time);
        }
        return $result;
    }
}
